package com.gitee.slowcreator.http.file.download;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.ReflectUtil;
import com.gitee.slowcreator.http.exception.HttpHandleException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zxw
 * @date 2023/6/10 19:46
 */
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class FileDownloadFactory {

    /**
     * 获取文件处理 默认单例
     *
     * @param fileDownloadClass 下载处理Class
     * @return
     */
    public static <Request, Response> IFileDownload<Request, Response> getHandle(Class<? extends IFileDownload> fileDownloadClass) {

        return getHandle(fileDownloadClass, false);
    }

    /**
     * 获取文件处理
     *
     * @param fileDownloadClass 下载处理Class
     * @param newInstance       是否生成一个新的实例 默认为单例
     * @return
     */
    public static <Request, Response> IFileDownload<Request, Response> getHandle(
            Class<? extends IFileDownload> fileDownloadClass, boolean newInstance) {

        if (fileDownloadClass == null) {
            log.error("新版下载请求未传入文件处理类");
            throw new HttpHandleException("下载请求处理不能为空！");
        }

        if (newInstance) {
            return ReflectUtil.newInstance(fileDownloadClass);
        } else {
            return Singleton.get(fileDownloadClass);
        }
    }
}
